package com.demo.demospringbootstarter.service;

import com.demo.demospringbootstarter.business.SmsService;
import com.demo.demospringbootstarter.entity.DemoProperties;
import com.demo.demospringbootstarter.util.spring.ApplicationContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;

public class DemoService {

    @Autowired
    private DemoProperties demoProperties;

    @Autowired
    @Qualifier("tx")
    private SmsService txsmsService;

    @Autowired
    @Qualifier("ali")
    private SmsService alismsService;

    public void test() {
        System.out.println(demoProperties.getName());
        System.out.println(demoProperties.getAliyun().getSignName());
    }

    public String send(String fromPhone, String toPhone, String content){
        //获取云厂商的业务实现类
        String type = demoProperties.getType();
        SmsService smsService = null;
        if (StringUtils.pathEquals(type, "tx")) {
            smsService = txsmsService;
        }
        if (StringUtils.pathEquals(type, "ali")) {
            smsService = alismsService;
        }
//        SmsService smsService = (SmsService) ApplicationContextHolder.getContext().getBean(type);

        return smsService.send(fromPhone,toPhone,content);
    }
}
